package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.Utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * 接口分类: admin  平台端的后端接口(管理员)
 * front  平台端的后端接口(用户)
 * inner  openfeign 远程调用接口
 * api    开放接口
 * <p>
 * 医院设置表 前端控制器
 * </p>
 * <p>
 * 使用Ruesult结果类接收统一返回值结果
 *
 * @author atguigu
 * @since 2022-02-11
 */

@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class AdminHospitalSetController {
    @Autowired

    private HospitalSetService hospitalSetService;

    /**
     * 查询所有医院设置信息
     *
     * @return
     */
    @ApiOperation(value = "医院设置列表")
    @GetMapping("findAll")
    public R findeAll() {
        List<HospitalSet> list = hospitalSetService.list();

        return R.ok().data("list", list).message("查询成功");
    }

    /**
     * 根据ID删除医院设置
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "医院设置删除")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(value = "ID", required = true)
                             @PathVariable Long id) {

        boolean bool = hospitalSetService.removeById(id);

        if (bool) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    /**
     * 分页查询 AND 条件查询
     * 调用 分页查询插件 和 queryWragger 条件包装器
     *
     * @param page
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @ApiOperation(value = "分页医院设置列表")
    @GetMapping("{page}/{limit}")
    public R pageList(@ApiParam(value = "当前页码", required = true)
                           @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true)
                           @PathVariable Long limit,
                      @ApiParam(value = "查询对象")
                                   HospitalSetQueryVo hospitalSetQueryVo
    ) {
        Page<HospitalSet> pageParams = hospitalSetService.selectPage(page, limit, hospitalSetQueryVo);

        return R.ok()
                .data("list", pageParams.getRecords())
                .data("total", pageParams.getTotal());
    }

    /**
     * 使用MD5加密器加密新增
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation(value = "新增医院设置")
    @PostMapping("saveHospSet")
    public R save(
            @ApiParam(value = "医院设置对象", required = true)
            @RequestBody HospitalSet hospitalSet) {

        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥  具体自己添加条件,选择了 当前时间+随机UUID
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + UUID.randomUUID()));

        boolean result = hospitalSetService.save(hospitalSet);
        if (result) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    /**
     * 根据ID查询医院设置
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询医院设置")
    @GetMapping("getHospSet/{id}")
    public R getById(
            @ApiParam(value = "医院设置ID", required = true)
            @PathVariable String id) {

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item", hospitalSet);
    }

    /**
     * 根据ID修改医院设置
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation(value = "根据ID修改医院设置")
    @PutMapping("updateHospSet")
    public R updateById(
            @ApiParam(value = "医院设置对象", required = true)
            @RequestBody HospitalSet hospitalSet) {
        boolean result = hospitalSetService.updateById(hospitalSet);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }

    /**
     * 批量删除医院设置
     *
     * @param idList id集合/数组  具体根据前端要求
     * @return
     */
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public R updateById(
            @ApiParam(value = "ID列表", required = true) List<Long> idList) {
        boolean batchRemove = hospitalSetService.removeByIds(idList);
        if (batchRemove) {
            return R.ok().message("批量删除成功");
        } else {
            return R.error().message("批量删除失败");
        }
    }

    /**
     * 医院设置锁定和解锁
     * 本质上就是根据ID 修改对应医院设置的 status
     * baseMapper 提供的方法 int updateById(@Param(Constants.ENTITY) T entity);
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(
            @ApiParam(value = "id", required = true) @PathVariable Long id,
            @ApiParam(value = "状态", required = true) @PathVariable Integer status
    ) {
        Boolean result = hospitalSetService.updateHospitalSet(id, status);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }
}
